/**
 * Created by wenchuan on 2018/5/5.
 */
define(function () {

    var self,
        app = win.app,
        orders = [],
        address = {
            GWCO013: '', //经销商手机号
            GWCO011: '',//经销商编号
            GWCM020: '',//详细地址
            GWCO012: '',//联系人
            GWCO021: '',//
            GWCO022: '',//
            GWCO023: '',//
            GWCO024: '',//
            GWCO025: '',//
            GWCO026: '',//
        },
        others = {
            countAmount: 0
        },
        fromOrder,
        orderSubmitArgs
    ;

    function setCountAmount() {
        var c = 0;
        orders.filterKV({
            ischecked: '1'
        }).forEach(function (v) {
            c += (Number(v.GWCM004) * Number(v.GWCM003));
        });
        others.countAmount = c;
    }

    var OrderSubmit = app.Module.extend({

        init: function () {
            self = this;
            orderSubmitArgs = app.pageParameters['zpordersubmit_page'];
            fromOrder = orderSubmitArgs.items[0];
            var items = orderSubmitArgs.items;
            items.forEach(function (p1, p2, p3) {
                var _p1 = app.cloneJSON(p1);
                delete _p1.details;
                _p1.ischecked = '1';
                p1.details.forEach(function (p10, p20, p30) {
                    orders.push(app.extend(p10, _p1));
                });
            });
            self._super({
                _tag: 'p_ordersubmit'
            }, win);
        },
        _lazy: function () {
            this._super();
            setCountAmount();
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        subData: function () {

            address.orders = orders.filterKV({
                ischecked: '1'
            });
            address.GWCO006 = fromOrder.GWCM006;//线人
            address.GWCO007 = fromOrder.GWCM007;//线人手机
            if (address.orders.length == 0) {
                return;
            }

            app.ajax({
                xurl: "api/wx/gwat200/yxsjzpSave",
                loading: true,
                xdata: {
                    obj: app.jsonString(address)
                },
                confirm: {
                    msg: '确认提交派单？'
                },
                before: function (b) {
                    if (!address.GWCO011 || address.GWCO011 == '') {
                        app.toast('请选择经销商！');
                        b = false;
                    }
                    return b;
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '派单提交成功！',
                            cb: function () {
                                app.back(2);
                            }
                        });
                        orderSubmitArgs.cb();

                    }else if(result.data.appcode==501){
                        app.alert({
                            msg: result.data.appmsg
                        });
                    }
                }
            });
        },
        _ready: function () {
            Vue.component('ordersubmit-template', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                methods: {
                }
            });

            new Vue({
                el: app.getByid(doc, 'address-box'),
                data: {
                    address: address
                },
                methods: {
                    showDistributors: function () {
                        app.openPage({
                            src: "yxsjzp/distributor.html",
                            argsKey: 'distributor_page',
                            args: {
                                cb: function (obj) {
                                    address.GWCO011 = obj.GWAK001;
                                    address.GWCO012 = obj.GWAK031;
                                    address.GWCM020 = obj.GWAK028;
                                    address.GWCO013 = obj.GWAK030;
                                    address.GWCO021 = obj.GWAK020;
                                    address.GWCO022 = obj.GWAK021;
                                    address.GWCO023 = obj.GWAK022;
                                    address.GWCO024 = obj.GWAK023;
                                    address.GWCO025 = obj.GWAK024;
                                    address.GWCO026 = obj.GWAK025;
                                }
                            }
                        });
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'contentCenter'),
                data: {
                    items: orders
                },
                methods: {}
            });

            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    subOrder: function () {
                        self.subData();
                    }
                }
            });

        }
    });

    return new OrderSubmit();

});